home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4161 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  74 lines

  1. Newsgroups: comp.lang.c
  2. Path: usenet.eel.ufl.edu!warwick!bsmail!talisker!nathan
  3. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  4. Subject: Re: Float calculations
  5. Message-ID: <DM5AtL.C24@uns.bris.ac.uk>
  6. Sender: usenet@uns.bris.ac.uk (Usenet news owner)
  7. Nntp-Posting-Host: talisker.pact.srf.ac.uk
  8. Organization: Inmos
  9. X-Newsreader: TIN [version 1.2 PL2]
  10. References: <4eqssf$d9q@camelot.ccs.neu.edu> <DM458u.F1y@microunity.com>
  11. Date: Fri, 2 Feb 1996 11:05:44 GMT
  12.  
  13. In article <4eqssf$d9q@camelot.ccs.neu.edu>, jason@ccs.neu.edu (Jason Leatherman) writes:
  14. |>   float a, b;
  15. |> 
  16. |>   printf("%0.10f  %0.10f  %0.10f\n", 99974.0, 50.0, 99974.0/50.0);
  17. |> 
  18. |>   a = 99974.0;
  19. |>   b = 50.0;
  20. |>   printf("%0.10f  %0.10f  %0.10f\n", a, b, a/b);
  21. |> The output is:
  22. |> 99974.0000000000  50.0000000000  1999.4800000000
  23. |> 99974.0000000000  50.0000000000  1999.4799804688
  24. |> 
  25. |>   Why do the divisions produce different results?  This is probably some
  26.  
  27. What your really asking is what is the difference between
  28.     99974.0/50.0
  29. and
  30.     a = 99974.0, b = 50.0, a/b
  31.  
  32. The constants have the type double and so the first division is performed
  33. at double precision. The assignments to a and b convert the doubles to
  34. floats. Now the division of a by b behaves differently in K&R to ANSI.
  35.  
  36. In K&R all floating point expressions are evaluated at double precision.
  37. As the values of a and b are exactly representable at float precision, you'll
  38. get the same result.
  39.  
  40. In ANSI floating point expressions are evaluated at float precision, if
  41. both operands are of type float. If one is of type double, then the
  42. operation happens at double precision. So in ANSI a/b has a result of type
  43. float, and in this case the exact result is not representable in a float.
  44.  
  45. Now with optimization things get a little weirder (hence the
  46. -fstore-floats option on gcc). The optimizer might realize that
  47. the result of an expression assigned to a float is still
  48. sitting in floating point registers, and use that value, rather than
  49. refetching the value from the variable. Now some hardware keeps floating
  50. point numbers in registers in a canonical long double form. This could
  51. mean that the value in the register is different to the value in the
  52. variable (because it has not been rounded). Most of the time this
  53. extra precision only helps, but in some cases it breaks code (particularly
  54. that which is looking for the smallest representable float value).
  55.  
  56. for instance
  57.     float a;
  58.     double b, c;
  59.  
  60.     b = 99974.0/50.0;
  61.     a = b;
  62.     c = a;
  63.  
  64. a and b will be different (provided a's been stored), but
  65. will c be equal to a or to b?
  66.  
  67. nathan
  68.  
  69. --
  70. Nathan Sidwell                         Holder of the Xmris home page
  71. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  72. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  73. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  74.